package org.git.movie.videojiexi.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.git.movie.videojiexi.enums.VideoChannel;
import org.git.movie.videojiexi.model.VideoModel;
import org.git.movie.videojiexi.service.VideoService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.net.URLDecoder;
import java.util.Map;

@Service("SougouServiceImpl")
public class SougouServiceImpl implements VideoService {
    private static Logger log = LoggerFactory.getLogger(SougouServiceImpl.class);
    /**
     * 如何获取视频地址：
     * 在视频上-->点击鼠标右键-->复制视频页面地址
     * @param url
     * @param userAgent
     * @return
     * @throws Exception
     */
    @Override
    public VideoModel parseUrl(String url, String userAgent) throws Exception {

        String htmlUrl = url;

        Document doc = null;
        try {
            doc = Jsoup.connect(htmlUrl).get();
        } catch (Exception e) {
            ///e.printStackTrace();
            System.out.println(e.getMessage()+":--------------->"+url);
        }
        if( doc == null )return  null;
        Elements elScripts = doc.getElementsByTag("script");
        String vcontent = null;
        for(Element scriptItem :elScripts){
            String content =  scriptItem.data().toString();
            if(content != null && content.indexOf("window.__INITIAL_STATE__") >= 0){
                vcontent = content.trim();
                break;
            }
        }
        if(vcontent != null){
            String[] v0Array = vcontent.split(";\\(function");
            vcontent = v0Array[0].replace("window.__INITIAL_STATE__=","");
            if(vcontent.endsWith(";")){
                vcontent =vcontent.substring(0,vcontent.length() -1);
            }
        }
        JSONObject jsonObject = JSON.parseObject(vcontent);
JSONObject __INITIAL_STATE__ = jsonObject.getJSONObject("__INITIAL_STATE__");
        JSONObject articleResult = __INITIAL_STATE__.getJSONObject("articleResult");
        String title = articleResult.getString("title");

        JSONObject cover_imageObj = articleResult.getJSONObject("cover_image");
        String imgUrl = cover_imageObj.getString("url");
        imgUrl = URLDecoder.decode(imgUrl,"UTF-8");



        String sPlayUrl =  articleResult.getString("url");

        sPlayUrl = URLDecoder.decode(sPlayUrl,"UTF-8");

//
        VideoModel rep = new VideoModel();
//        rep.setCover(picUrl);
//        rep.setFileType(fileType);
        rep.setPlayAddr(sPlayUrl);

        rep.setChannel(VideoChannel.SoGou);
        rep.setName(title);
        rep.setCover(imgUrl);

        return rep;
    }



    public static void main(String[] args) throws Exception {
//        VideoModel rep =   new TencentQqServiceV2Impl().parseUrl("https://v.html5.qq.com/node/h5videoPlayer?vid=5984091005311059307&ch=&preloadShell=1&useShell=1&newSharePage=1&extinfo=ch%3D001500&topicid=151&sch=001505&invokeBy=&sc_id=3ORstoD","");
//        log.info(JSON.toJSONString(rep));
        VideoModel rep2 =   new SougouServiceImpl().parseUrl("https://wapv.sogou.com/sgsearch/sgs_video.php?docId=20129b041JDx2X&mat=12","");
        log.info(JSON.toJSONString(rep2));
    }
}
